/**
 * Created by Administrator on 2017/5/23 0023.
 */
$(function(){


    buycart();

    function buycart(){
        var totalc = 0;
        var total = 0;
        $(".tab").empty();
        var obf = $.cookie("cart") ? JSON.parse($.cookie("cart")) : [];

        // console.log(obf.length);
        console.log(obf);
        for(var i = 0; i < obf.length; i++){
            var tmkhtml = '<tbody class="tab">';
            tmkhtml += '<tr>';
            tmkhtml += '<td class="bt_td"><i class="check_b"></i></td>';
            tmkhtml += '<td class="tib"><a class="tib_bg"><img src="' + obf[i].url + '"></a><div><span>' + obf[i].proname + '</span><span>' + obf[i].bnef + '</span></div></td>';

            tmkhtml += '<td>' + obf[i].color1 + '</td>';
            tmkhtml += '<td>' + obf[i].price + '</td>';
            tmkhtml += '<td><div class="qu_r"><input type="button" value="-" class="reduce"><input type="text" value="' + obf[i].cartnumber + '" class="number"><input type="button" value="+" class="add"></div></td>';
            tmkhtml += '<td style="color:#f54747;">' + obf[i].pricer*obf[i].cartnumber + '</td>';
            tmkhtml += '<td><a class="delete" style="color:#f54747;">删除</a></td>';
            tmkhtml += '</tr>';
            tmkhtml += '</tbody>';
            $(".table").append(tmkhtml);

            totalc += parseInt(obf[i].pricer * obf[i].cartnumber);

            total += parseInt(obf[i].pricer * obf[i].cartnumber);
        }
        $(".total_c").html(totalc);
        $(".total").html(total);

    }

    /*删除按钮*/
    $(".main_inner").on("click", ".delete", function(){
        var bool = confirm("确定要删除该商品吗?");
        var arr = JSON.parse($.cookie("cart"));

        var index = $(this).data().i;
        if(bool){
            arr.splice(index,1)
            $.cookie("cart",JSON.stringify(arr),{expires:30, path:"/"})
            buycart();
        }
    })

    //减号
    $(".main_inner").on("click", ".reduce", function(){
        if(parseInt($(this).next("input").val()) == 1){
            $(this).next("input").attr("disable", true);
            return;
        }

        var arr = JSON.parse($.cookie("cart"));
        var index = $(this).index(".reduce");
        arr[index].cartnumber--;
        console.log(arr[index].cartnumber)
        //			$("span[data-shu="+index+"]").html(arr[index].num)
        $.cookie("cart", JSON.stringify(arr), {expires : 30, path : "/"})
        buycart();
    })

    //加号
    $(".main_inner").on("click", ".add", function(){
        var arr = JSON.parse($.cookie("cart"));
        var index = $(this).index(".add");
        console.log(index)
        console.log(arr[index].cartnumber)
       var aaaa  = arr[index].cartnumber++;

        //			$("span[data-shu="+index+"]").html(arr[index].num)
        $.cookie("cart", JSON.stringify(arr), {expires : 30, path : "/"})
        buycart();
    })



})